package website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.constants.Code;
import website.domain.*;
import website.mapper.LinkMapper;
import website.mapper.PhotoMapper;
import website.mapper.UserMapper;
import website.service.LinkService;
import website.service.PhotoService;
import website.service.WebService;
import website.utils.NowDateUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {

    @Resource
    private PhotoMapper photoMapper;

    @Autowired
    private WebService webService;

    /**
     * 查询照片
     * @return
     */
    @Override
    public ResModel<List<Photo>> getList() {
        //更新网站浏览量
        webService.update().setSql("visits = visits + 1").update();
        return ResModel.success(Code.SUCCESS,list());
    }

    /**
     * 分页查询相册
     * @param current
     * @param size
     * @return
     */
    @Override
    public ResModel<IPage<Photo>> getByPage(Integer current, Integer size) {
        Page<Photo> page = new Page<>(current, size);
        Page<Photo> messagePage = photoMapper.selectPage(page, null);
        return ResModel.success(Code.SUCCESS,messagePage);
    }

    /**
     * 删除照片
     * @param id
     * @return
     */
    @Override
    public ResModel delPhoto(Integer id) {
        if (id!=null){
            int i = photoMapper.deleteById(id);
            return ResModel.success(Code.DELETE,i);
        }
        return ResModel.error(Code.FAIL);
    }

    /**
     * 添加照片
     * @param photo
     * @return
     */
    @Override
    public ResModel addPhoto(Photo photo) {
        if (photo.getSrc()!=null){
            photo.setIssueTime(NowDateUtils.getNowDate());
            boolean save = save(photo);
            return ResModel.success(Code.INSERT,save);
        }
        return ResModel.error(Code.FAIL);
    }

    /**
     * 分页查询自己相册
     * @param current
     * @param size
     * @param uid
     * @return
     */
    @Override
    public ResModel<IPage<Photo>> listByUidPhotoList(Integer current, Integer size, Integer uid) {
        Page<Photo> page = new Page<>(current, size);
        LambdaQueryWrapper<Photo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,Photo::getUid,uid);
        Page<Photo> res = page(page, wrapper);
        return ResModel.success(Code.SUCCESS,res);
    }
}
